true

I. Timeline to represent stages and Gantt

A good example of this, it is the representation of the stages in a research. In this case, it is represented four big stages of my PhD thesis research: Theoretical frame, Fieldwork, Analysis and Writing. Something benefitial of using this package is it allows a simple and comfortably visual with a simple code.

Firts, load the package “vistime”.

#install.packages("vistime")
library("vistime")

Second, construct the data frame.

timeline_data <- data.frame(event = c("Theoretical frame", "Fieldwork", "Analysis", "Writing"), 
                            start = c("2018-09-01", "2019-09-01", "2019-12-01", "2020-01-02"), 
                            end = c("2019-09-01", "2020-01-15", "2020-10-01", "2021-09-01"), 
                            group = "Stages of 
                            the research")

Third, select the better option for your research: static or interactive visualizations.

A static option of the visualization:

gg_vistime(timeline_data) 

An interactive option of the same visualization:

hc_vistime(timeline_data)

There is another interactive option:

vistime(timeline_data)

Arguments of each function

II. Timeline to represent many specific events with a difference by types.

For this timeline, I used the ones done in the next webpage: https://www.themillerlab.io/post/timelines_in_r/

I used to work with many life history at the same time. And maybe you would be able to manage well with less than ten cases, however, when you got more than that it starts to get more problematic. Therefore, a useful way to work with all those cases is using one timeline for each one. It is important to highlith that this is a complementary tool for the researcher, however, it is not a repleasement of the deep information of a life history.

library(scales)
library(lubridate)
library(ggplot2)
library(tidyverse)
library(knitr)
library(timevis)

First, let’s create a database.

In this particular case, I will used as an example the academic event in which I had participated in one year.

CaseFrancisca <- data.frame(
  Year = c(rep(c(2019), times =6), rep(c(2020), times =7)), 
  Months = c(4,6,6,7,10,10,5,7,7,7,7,7,7), 
  Days = c(24,7,13,9,1,3,20,2,7,13,14,15,23),
  Milestones = c("BSA Annual Conference", "PGR Sociology Conference", 
                 "MICRA Conference", "BSG Conference", "MICRA", "PhD Colloquium", "MICRA Conference", "BSG Conference", "Jornada de Redes 2020", "Panel Hispanic SUNBELT","SUNBELT", "SUNBELT", "ELSOC"), 
  Event_type= c("Presenter", "Presenter", "Presenter", "Presenter", "Organizer", "Organizer", "Organizer", "Presenter","Organizer","Organizer","Presenter", "Presenter", "Presenter")) 

Add one column of the date.

CaseFrancisca$date <- with(CaseFrancisca, ymd(sprintf('%04d%02d%02d', CaseFrancisca$Year, CaseFrancisca$Months, CaseFrancisca$Days))) 
CaseFrancisca <- CaseFrancisca[with(CaseFrancisca, order(date)), ]
kable(head(CaseFrancisca))
Year Months Days Milestones Event_type date
2019 4 24 BSA Annual Conference Presenter 2019-04-24
2019 6 7 PGR Sociology Conference Presenter 2019-06-07
2019 6 13 MICRA Conference Presenter 2019-06-13
2019 7 9 BSG Conference Presenter 2019-07-09
2019 10 1 MICRA Organizer 2019-10-01
2019 10 3 PhD Colloquium Organizer 2019-10-03

Add one colours per type of event. In this case I used my role at the academic event, as a presenter or organizer.

Event_type_levels <- c("Presenter", "Organizer") 
Event_type_colors <- c("#C00000", "#FFC000") 

Define the vectors of the events, and heigths of the milestones

CaseFrancisca$Event_type <- factor(CaseFrancisca$Event_type, levels= Event_type_levels, ordered=TRUE)
positions <- c(0.5, -0.5, 1.0, -1.0, 1.25, -1.25, 1.5, -1.5) 
directions <- c(1, -1) 

Add positions and directions:

line_pos <- data.frame(
  "date"=unique(CaseFrancisca$date),
  "position"=rep(positions, length.out=length(unique(CaseFrancisca$date))),
  "direction"=rep(directions, length.out=length(unique(CaseFrancisca$date))))
CaseFrancisca <- merge(x=CaseFrancisca, y=line_pos, by="date", all = TRUE) 

Create a one month “buffer” at the start and end of the timeline

month_buffer <- 1 
month_date_range <- seq(min(CaseFrancisca$date) - months(month_buffer), max(CaseFrancisca$date) + months(month_buffer), by='month')

Eddit the format of months, in three letter abbreviations and range:

month_format <- format(month_date_range, '%b') 
month_df <- data.frame(month_date_range, month_format)
year_date_range <- seq(min(CaseFrancisca$date) - months(month_buffer), max(CaseFrancisca$date) + months(month_buffer), by='year')

Add format of year label

year_date_range <- as.Date(
  intersect(
    ceiling_date(year_date_range, unit="year"),
    floor_date(year_date_range, unit="year")),  
  origin = "1970-01-01") 
year_format <- format(year_date_range, '%Y') 
year_df <- data.frame(year_date_range, year_format)

Now for doing the plot, there are seven steps:
1. Create timeline coordinates and label milestones.

timeline_plot<-ggplot(CaseFrancisca,aes(x=date,y= position, 
                                 col=Event_type, label=CaseFrancisca$Milestones)) 
timeline_plot<-timeline_plot+labs(col="Milestones") 
  1. Add colors and order of the milestones.
timeline_plot<-timeline_plot+scale_color_manual(values=Event_type_colors, labels=Event_type_levels, drop = FALSE) 
timeline_plot<-timeline_plot+theme_classic() 
timeline_plot<-timeline_plot+geom_hline(yintercept=0, color = "black", size=0.3)
  1. Plot the vertical lines for the events, and remove X.
timeline_plot<-timeline_plot+geom_segment(data=CaseFrancisca, aes(y=CaseFrancisca$position,yend=0,xend=CaseFrancisca$date), color='black', size=0.2) 
timeline_plot<-timeline_plot+geom_point(aes(y=CaseFrancisca$position), size=3) 
timeline_plot<-timeline_plot+theme(axis.line.y=element_blank(),
                                   axis.text.y=element_blank(),
                                   axis.title.x=element_blank(),
                                   axis.title.y=element_blank(),
                                   axis.ticks.y=element_blank(),
                                   axis.text.x =element_blank(),
                                   axis.ticks.x =element_blank(),
                                   axis.line.x =element_blank(),
                                   legend.position = "bottom") 
timeline_plot

  1. Add text in each month and years.
timeline_plot<-timeline_plot+geom_text(data=month_df, aes(x=month_date_range,y=-0.15,label=month_format),size=3.5,vjust=0.5, color='black', angle=90) 
timeline_plot<-timeline_plot+geom_text(data=year_df, aes(x=year_date_range, y=-0.25,label=year_format, fontface="bold"),size=3.5, color='black') 
  1. Add labels for each milestone event.
text_offset <- 0.2 
absolute_value<-(abs(CaseFrancisca$position)) 
text_position<- absolute_value + text_offset
CaseFrancisca$text_position<- text_position * CaseFrancisca$direction 

6.Now we can add the labels to the timeline for our milestones.

timeline_plot<-timeline_plot+geom_text(aes(y=CaseFrancisca$text_position, label=CaseFrancisca$Milestones),size=3.5, vjust=0.6)
  1. Plot! Plot as a static timeline
timeline_plot

Plot an interactive timeline

#install.packages("plotly")
library(plotly)
ggplotly(timeline_plot)